<?php
/**
 * Created by PhpStorm
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Storepp extends Base {
    /**
     *
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $user_name = trim(input('user_name'), ' ');
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['spp_name|spp_remark|t_name|pp_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($typeid) && $typeid != ''){
                $where['spp_t_id'] = $typeid;
            }
            if(isset($s_pp_id) && $s_pp_id != ''){
                $where['spp_pp_id'] = $s_pp_id;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_store_pps')->alias('st')
                ->join('up_types t','st.spp_t_id=t.t_id','LEFT')
                ->join('up_pp pp','st.spp_pp_id=pp.pp_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_store_pps')->alias('st')
                ->join('up_types t','st.spp_t_id=t.t_id','LEFT')
                ->join('up_pp pp','st.spp_pp_id=pp.pp_id','LEFT')
                ->where($where)
                ->order('spp_sort desc,spp_time asc')
                ->page($NowPage,$limits)
                ->field('st.*,t_name,pp_name')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $datas = $this->getTypePPStyle();
        $this->assign('type1',$datas[3]);
        $this->assign('pp1',$datas[4]);
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'spp_t_id' => $t_id,
                'spp_pp_id' => $pp_id,
                'spp_name' => $spp_name,
                'spp_remark' => $spp_remark,
                'spp_sort' => $spp_sort,
                'spp_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_store_pps')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $datas = $this->getTypePPStyle();
        $this->assign('type1',$datas[3]);
        $this->assign('pp1',$datas[4]);
        return $this->fetch();
    }
    /**
     * 编辑
     * @return mixed
     */
    public function typeEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'spp_t_id' => $t_id,
                'spp_pp_id' => $pp_id,
                'spp_name' => $spp_name,
                'spp_remark' => $spp_remark,
                'spp_sort' => $spp_sort,
                'spp_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_store_pps')->where('spp_id',$spp_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $room_id = input('spp_id');
        $info = Db::table('up_store_pps')
            ->where('spp_id',$room_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        $datas = $this->getTypePPStyle($info['spp_t_id']);
        $this->assign('type1',$datas[3]);
        $this->assign('pp1',$datas[5]);
        return $this->fetch();
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'spp_sort'=>input('value'),
        ];
        $res = Db::table('up_store_pps')->where('spp_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function typeDel(){
        $nt_id = input('id');
        $res = Db::table('up_store_pps')->where('spp_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 冻结启用
     * @return mixed|\think\response\Json
     */
    public function changeStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'spp_status'=>input('num'),
        ];
        $res = Db::table('up_store_pps')->where('spp_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}